<template>
  <div>
    <div class="app-box">
      <div class="attrTitle">基本信息</div>
      <a-divider></a-divider>
      <j-form-container :disabled="true">
        <a-form-model ref="mainForm" :model="hteFile">
          <a-row :gutter="24">
            <a-col v-if="hteFile.projectNo" :span="12">
              <a-form-model-item v-bind="formItemLayout" label="文件名称" tyle="width: 100%" prop="fileName">
                <a-textarea v-model="hteFile.fileName" :auto-size="true" />
              </a-form-model-item>
            </a-col>
            <a-col v-else :span="24">
              <a-form-model-item
                v-bind="{
                  labelCol: {
                    xs: { span: 24 },
                    md: { span: 4 },
                  },

                  wrapperCol: {
                    xs: { span: 24 },
                    md: { span: 18 },
                  },
                }"
                label="文件名称"
                tyle="width: 100%"
                prop="fileName"
              >
                <a-textarea v-model="hteFile.fileName" :auto-size="true" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12" v-if="hteFile.projectNo">
              <a-form-model-item v-bind="formItemLayout" label="项目编号" tyle="width: 100%">
                <a-input v-model.trim="hteFile.projectNo" disabled />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="12">
              <a-form-model-item v-bind="formItemLayout" label="文件类别" tyle="width: 100%" prop="fileType">
                <a-tree-select
                  v-model="hteFile.fileType"
                  :tree-data="treeData"
                  placeholder="请选择文件类别"
                  tree-default-expand-all
                  @change="handleFileType"
                >
                </a-tree-select>
              </a-form-model-item>
            </a-col>

            <a-col :span="12">
              <a-form-model-item v-bind="formItemLayout" label="文件编号" tyle="width: 100%">
                <a-input v-model.trim="hteFile.fileNo" placeholder="不填写将会自动生成" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="24" v-if="parentId != 2">
              <a-form-model-item
                v-bind="{
                  labelCol: {
                    xs: { span: 24 },
                    md: { span: 4 },
                  },

                  wrapperCol: {
                    xs: { span: 24 },
                    md: { span: 18 },
                  },
                }"
                label="关键词"
                tyle="width: 100%"
                prop="keywords"
              >
                <a-textarea v-model="hteFile.keywords" :auto-size="true" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24" v-if="parentId != 2">
              <a-form-model-item
                v-bind="{
                  labelCol: {
                    xs: { span: 24 },
                    md: { span: 4 },
                  },

                  wrapperCol: {
                    xs: { span: 24 },
                    md: { span: 18 },
                  },
                }"
                label="文件中文摘要"
                tyle="width: 100%"
                prop="purpose"
              >
                <a-textarea v-model="hteFile.purpose" :auto-size="true" placeholder="" />
              </a-form-model-item>
            </a-col>

            <a-col :span="12">
              <a-form-model-item v-bind="formItemLayout" label="版本号" tyle="width: 100%">
                <a-input v-model="hteFile.version" disabled placeholder="自动生成" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item v-bind="formItemLayout" label="创建人" tyle="width: 100%">
                <a-input v-model="hteFile.createUser" disabled placeholder="自动生成" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item v-bind="formItemLayout" label="发布人" tyle="width: 100%">
                <a-input v-model="hteFile.publishUser" disabled placeholder="自动生成" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item v-bind="formItemLayout" label="发布部门" tyle="width: 100%">
                <a-input v-model="hteFile.publishDept" disabled placeholder="自动生成" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item v-bind="formItemLayout" label="发布生效时间" tyle="width: 100%">
                <a-input v-model="hteFile.effectTime" disabled placeholder="自动生成" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item v-bind="formItemLayout" label="创建部门" tyle="width: 100%">
                <a-input v-model="hteFile.addDept" disabled placeholder="自动生成" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <!-- <a-row>
            <a-col :span="24">
              <a-form-model-item
                prop="purpose"
                :labelCol="{ md: 4, xs: 24 }"
                :wrapperCol="{ md: 18, xs: 24 }"
                label="文件用途"
                style="width: 100%"
              >
                <a-textarea v-model="hteFile.purpose" />
              </a-form-model-item>
            </a-col>
          </a-row> -->
        </a-form-model>
      </j-form-container>
      <div class="attrTitle">关键信息</div>
      <a-divider></a-divider>
      <div class="attr-box" v-show="classAttrList.length > 0">
        <libFileAttr
          v-if="parentId == 2"
          ref="fileattr"
          :classAttrList="classAttrList"
          :formItemLayout="formItemLayout"
          :parentId="parentId"
          :formDisabled="true"
        ></libFileAttr>
        <FileAttrNormal
          v-if="parentId == 4"
          ref="fileattr"
          :classAttrList="classAttrList"
          :formItemLayout="formItemLayout"
          :parentId="parentId"
          :formDisabled="true"
        ></FileAttrNormal>
        <!-- <a-form-item
            v-bind="formItemLayout"
            class="attr-item"
            v-for="(item, key) in classAttrList"
            :key="key"
            :label="item.name"
          >
            {{ item.content }}
          </a-form-item> -->
      </div>
    </div>
  </div>
</template>

<script>
import { getAction } from '@/api/manage'
import moment from 'moment'
import LibFileAttr from '@/views/library/components/LibFileAttr'
import FileAttrNormal from '@/views/library/components/FileAttrNormal'
export default {
  components: { LibFileAttr, FileAttrNormal },
  props: {
    parentId: {
      type: String,
      default: null,
    },
    projectNo: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      formItemLayout: {
        labelCol: {
          xs: { span: 18 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 20 },
          sm: { span: 12 },
        },
      },
      hteFile: {},
      classAttrList: [],
      urls: {
        listAttr: '/lib/hteLibFile/attrs',
        getTreeList: '/lib/hteFileClass/getTreeList',
      },
      form: this.$form.createForm(this, { name: 'form' }),
      treeData: [],
    }
  },
  created() {
    this.fetchClassTree()
  },
  methods: {
    /**
     * 获取分类扩展属性
     */
    fetchClassAttr(classId, fileId, version) {
      if (this.hteFile.fileType) {
        let classId = this.hteFile.fileType
        getAction(this.urls.listAttr + '?classId=' + classId + '&fileId=' + fileId + '&version=' + version, {}).then(
          (res) => {
            if (res.success) {
              this.classAttrList = res.result.filter((item) => {
                return !!item.showInDetails
              })
            } else {
              this.$message.error('获取分类拓展属性错误')
            }
          }
        )
      } else {
        this.classAttrList = []
      }
    },

    setMain(hteFile) {
      this.hteFile = hteFile
      this.fetchClassAttr(this.hteFile.fileType, this.hteFile.id, this.hteFile.version)
      // this.checkFileType()
    },
    setClassAttr(attrs) {
      this.classAttrList = attrs
    },
    handleFileType(value) {
      this.fetchClassAttr(this.hteFile.fileType, this.hteFile.id, this.hteFile.draftVersion)
      this.checkFileType()
    },
    checkFileType() {
      let currentFileType = this.getFileType(this.treeData)

      while (
        currentFileType &&
        ['学位论文', '电子期刊'].indexOf(currentFileType.title) < 0 &&
        currentFileType.parentId
      ) {
        currentFileType = this.getParentFileType(this.treeData, currentFileType.parentId)
      }

      if (currentFileType && ['学位论文', '电子期刊'].indexOf(currentFileType.title) > -1) {
        return this.$set(this.hteFile, 'projectNo', this.projectNo)
      } else {
        this.$set(this.hteFile, 'projectNo', '')
      }
    },
    getFileType(list) {
      for (let l of list) {
        if (l.value == this.hteFile.fileType) {
          return l
        } else {
          if (l.children && l.children.length > 0) {
            return this.getFileType(l.children)
          }
        }
      }
    },
    getParentFileType(list, parentId) {
      for (let l of list) {
        if (l.value == parentId) {
          return l
        } else {
          if (l.children && l.children.length > 0) {
            return this.getParentFileType(l.children, parentId)
          }
        }
      }
    },
    /**
     * 获取分类树
     */
    fetchClassTree() {
      getAction(this.urls.getTreeList, { parentId: this.parentId }).then((res) => {
        if (res.success) {
          this.treeData = res.result.treeList
        } else {
          this.$message.error('获取分类树错误')
        }
      })
    },
  },
}
</script>

<style>
.attr-box {
  width: 100%;
  /* display: flex;
  align-content: flex-start;
  flex-flow: row wrap; */
}

/* .attr-item {
  width: 50%;
  margin: 0;
  margin-bottom: 20px;
} */
.ant-form-item {
  margin-bottom: 5px !important;
}
.attrTitle {
  height: 40px;
  line-height: 40px;
  text-align: left;
  font-size: 20px;
  font-weight: bold;
  color: #000;
}
</style>
